home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / Notify.cls < prev    next >
Text File  |  1997-06-14  |  3KB  |  92 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "CFileNotify"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Option Explicit
  11.  
  12. Public Enum EErrorNotify
  13.     errFirst = 100
  14.     errInvalidDirectory = 100
  15.     errInvalidType = 101
  16.     errInvalidArgument = 102
  17.     errTooManyNotifications = 103
  18.     errNotificationNotFound = 104
  19.     errNoFileChangeMethod = 105
  20.     errLast = 105
  21. End Enum
  22.  
  23. Private Sub Class_Initialize()
  24.     BugMessage "Initialize object"
  25.     ' Count this object
  26.     cObject = cObject + 1
  27. End Sub
  28.  
  29. Private Sub Class_Terminate()
  30.     ' Uncount this object
  31.     cObject = cObject - 1
  32.     ' Main loop tests -1 because count is 0 to start
  33.     If cObject = 0 Then cObject = -1
  34. End Sub
  35.  
  36. Function Connect(notifier As IFileNotifier, sDir As String, _
  37.                  efn As EFILE_NOTIFY, fSubTree As Boolean) As Long
  38.     Connect = hInvalid ' Assume fail
  39.     Dim i As Long, h As Long
  40.     ' Find blank handle space
  41.     For i = 0 To cLastNotify
  42.         If ahNotify(i) = hInvalid Then
  43.             ' Set up notification
  44.             h = FindFirstChangeNotification(sDir, fSubTree, efn)
  45.             Connect = h
  46.             If h = hInvalid Then
  47.                 ' Change notification unsupported on remote disks
  48.                 If Err.LastDllError <> ERROR_NOT_SUPPORTED Then
  49.                     RaiseError errInvalidArgument
  50.                 End If
  51.                 Exit Function
  52.             End If
  53.             ' Store information
  54.             ahNotify(i) = h
  55.             With aconNotify(i)
  56.                 Set .notifier = notifier
  57.                 .sDir = sDir
  58.                 .efn = efn
  59.                 .fSubTree = fSubTree
  60.             End With
  61.             Exit Function
  62.         End If
  63.     Next
  64.     RaiseError errTooManyNotifications
  65. End Function
  66.  
  67. Function Disconnect(hNotify As Long) As Boolean
  68.     Dim i As Long, f As Boolean
  69.     For i = 0 To cLastNotify
  70.         If ahNotify(i) = hNotify Then
  71.             ' Destroy notification
  72.             f = FindCloseChangeNotification(hNotify)
  73.             Disconnect = True
  74.             ' Compact the arrays
  75.             Do While i <= cLastNotify
  76.                 If ahNotify(i) = hInvalid Then Exit Do
  77.                 ahNotify(i) = ahNotify(i + 1)
  78.                 With aconNotify(i)
  79.                     Set .notifier = aconNotify(i + 1).notifier
  80.                     .sDir = aconNotify(i + 1).sDir
  81.                     .efn = aconNotify(i + 1).efn
  82.                     .fSubTree = aconNotify(i + 1).fSubTree
  83.                 End With
  84.                 i = i + 1
  85.             Loop
  86.             Exit Function
  87.         End If
  88.     Next
  89.     RaiseError errNotificationNotFound
  90. End Function
  91. '
  92.